//
//  CourseEvaluateSelectStudentView.m
//  优学荟管理系统
//
//  Created by 星道 on 17/7/18.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "CourseEvaluateSelectStudentView.h"

@implementation CourseEvaluateSelectStudentView
{
    IBOutlet UITableView *_TableView;
    IBOutlet NSLayoutConstraint *_TableViewHeight;

    NSString *_Title;
    // 孩子信息列表
    NSMutableArray <NSString *>*_Arr;
    // 选中block
    void(^_SelectRow)(NSInteger);
}




#pragma makr - 创建并配置
// 创建
+ (instancetype)CourseEvaluateSelectStudentViewWithTitle:(NSString *)Title ChildArr:(NSArray <CourseEvaluateChildModel *> *)ChildArr SelectRow:(void(^)(NSInteger Item))SelectRow
{
    return [[self alloc] initWithTitle:Title ChildArr:ChildArr SelectRow:SelectRow];
}

+ (instancetype)CourseEvaluateSelectSubjectViewWithTitle:(NSString *)Title SubjectArr:(NSArray<NSString *> *)SubjectArr SelectRow:(void (^)(NSInteger))SelectRow
{
    return [[self alloc] initWithTitle:Title SubjectArr:SubjectArr SelectRow:SelectRow];
}

// 实例化
- (instancetype)initWithTitle:(NSString *)Title ChildArr:(NSArray <CourseEvaluateChildModel *> *)ChildArr SelectRow:(void(^)(NSInteger Item))SelectRow
{
    self = [[[NSBundle mainBundle] loadNibNamed:[CourseEvaluateSelectStudentView description] owner:nil options:nil] lastObject];
    if (self)
    {
        _Title = Title;
        _Arr = [NSMutableArray array];
        // 取对象
        [ChildArr enumerateObjectsUsingBlock:^(CourseEvaluateChildModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            // 赋
            [_Arr addObject:obj.child_petName?[NSString stringWithFormat:@"%@(%@)",obj.child_petName,obj.enrollInfoId]:[NSString stringWithFormat:@"%@(%@)",obj.child_account,obj.enrollInfoId]];
        }];
        _SelectRow = SelectRow;

         // 配置TableView
        _TableView.delegate = self;
        _TableView.dataSource = self;

        // frame
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT);

        // 加载
//        [[[UIApplication sharedApplication].windows lastObject] addSubview:self];
        [[UIApplication sharedApplication].keyWindow addSubview:self];

        // 隐藏自己
        self.alpha = 0;

        // tableView的高度
        CGFloat Height = _Arr.count * 44 + 44;
        _TableViewHeight.constant = Height <= SCREENH_HEIGHT - 88 ?  Height :  SCREENH_HEIGHT - 88;

        // 动画显示
        [self Show];
    }
    return self;
}

// 实例化
- (instancetype)initWithTitle:(NSString *)Title SubjectArr:(NSArray<NSString *> *)SubjectArr SelectRow:(void (^)(NSInteger))SelectRow
{
    self = [[[NSBundle mainBundle] loadNibNamed:[CourseEvaluateSelectStudentView description] owner:nil options:nil] lastObject];
    if (self)
    {
        _Arr = [NSMutableArray arrayWithArray:SubjectArr];
        _Title = Title;
        _SelectRow = SelectRow;

        // 配置TableView
        _TableView.delegate = self;
        _TableView.dataSource = self;

        // frame
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT);

        // 加载
        [[UIApplication sharedApplication].keyWindow addSubview:self];

        // 隐藏自己
        self.alpha = 0;

        // tableView的高度
        CGFloat Height = _Arr.count * 44 + 44;
        _TableViewHeight.constant = Height <= SCREENH_HEIGHT - 88 ?  Height :  SCREENH_HEIGHT - 88;

        // 动画显示
        [self Show];
    }
    return self;
}
#pragma mark - 动画显示和关闭
/**
 显示
 */
- (void)Show
{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
    }];
}

// 关闭
- (void)Close
{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
#pragma makr - TableView代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _Arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 拿Cell
    UITableViewCell *Cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (Cell == nil)
    {
        Cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"Cell"];
        Cell.textLabel.numberOfLines = 2;
        Cell.textLabel.font = [UIFont systemFontOfSize:14.f];
    }
    // 赋值
    Cell.textLabel.text = _Arr[indexPath.row];

    return Cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // block传值
    if (_SelectRow)
    {
        _SelectRow (indexPath.row);
    }
    // 关闭
    [self Close];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *Header = [[UIView alloc] init];
    Header.backgroundColor = ColorRGB(0, 0, 0);
    Header.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);

    UILabel *Label = [[UILabel alloc] init];
    Label.text = _Title;
    Label.frame = CGRectMake(8, 0, Header.frame.size.width, Header.frame.size.height);
    Label.textColor = ColorRGB(255, 255, 255);
    [Header addSubview:Label];
    return Header;
}

@end
